package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.FastDFSUtils;
import jodd.io.FileNameUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author gsy
 * @date 2021年08月25日22:18
 * Description
 */
@RestController
@RequestMapping("/admin/product")
public class FileUploadController {

    @Value("${fileServer.url}")
    private String url;

    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file){
        try {
            byte[] bytes = file.getBytes();
            //文件名
            String originalFilename = file.getOriginalFilename();
            String extension = FileNameUtil.getExtension(originalFilename);
            //文件上传
            String fileId = FastDFSUtils.uploadFile(bytes, extension);
            return Result.ok(url+fileId);
        } catch (IOException e) {
            e.printStackTrace();
        }
            return Result.fail();
    }
}
